package dao;

import domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    // 添加老师
    public boolean addTeacher(Teacher t) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            // 如果不满
            if (teacher == null) {
                index = i;
                break;
            }
        }
        // 如果满了
        if (index == -1) {
            // 返回 false
            return false;
        } else {
            // 将老师对象记录到相应的索引位置
            teachers[index] = t;
            return true; // 添加成功的提示
        }
    }

    // 查询
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    // 删除老师信息
    public void deleteTeacherById(String id) {
        // 1.查询id在数组中的索引位置
        int index = getIndex(id);
        // 2.将该索引位置的元素，使用null进行替换
        teachers[index] = null;
    }

    // 获取索引
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t != null && id.equals(t.getId())) {
                index = i;
                break;
            }
        }
        return index;
    }

    // 更新老师信息
    public void updateTeacher(String id, Teacher newTeacher) {
        int index = getIndex(id);
        teachers[index] = newTeacher;
    }
}
